﻿using CNKI.TPI.Web.Admin.Model;
using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Report.IDAO;
using CNKI.TPI.Web.Search.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace CNKI.TPI.Web.Report.DAO
{
    public class DBStatisticsDAO : BaseDAO, IDBStatisticsDAO
    {
        public DataTable GetDBAccessInfo(IList<string> dbcode, DateTime? startDate, DateTime? endDate)
        {
            if (dbcode == null || dbcode.Count == 0)
            {
                return null;
            }
            string sql = @"SELECT DbCode, sum(IFNULL(Search,0)) SearchCount,sum(IFNULL(Download,0)) Download, sum(IFNULL(Browse,0)) Browse FROM tpi.accessstatistics
                            where{0}
                            group by DbCode";
            string where = "";
            foreach (string item in dbcode)
            {
                where += " DbCode = '" + item + "' or";
            }
            where = where.Substring(0, where.Length - 3);

            if (startDate != null && endDate != null)
            {
                where = "(" + where + ")";
                where += " and date >= '" + ((DateTime)startDate).ToString("yyyy-MM-dd") + " 00:00:00' and date<='" + ((DateTime)endDate).ToString("yyyy-MM-dd") + " 23:59:59'";
            }
            else if (startDate != null)
            {
                where = "(" + where + ")";
                where += " and date >= '" + ((DateTime)startDate).ToString("yyyy-MM-dd") + " 00:00:00'";
            }
            else if (endDate != null)
            {
                where = "(" + where + ")";
                where += " and date <= '" + ((DateTime)endDate).ToString("yyyy-MM-dd") + " 23:59:59'";
            }
            return Context.ExecuteSql(string.Format(sql, where), "mysql");
        }
        /// <summary>
        /// 获取分类
        /// </summary>
        /// <returns></returns>
        public IList<SYS_CMS_RESOURCE_TEMPLATE> GetTemplateClS()
        {
            IList<SYS_CMS_RESOURCE_TEMPLATE> result = null;
            try
            {
                result = Context.GetAll<SYS_CMS_RESOURCE_TEMPLATE>();
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }

        public DataTable GetDBStatisticsInfo(string dbCode, DateTime? startDate, DateTime? endDate)
        {
            return null;        }
        /// <summary>
        /// 根据数据库编码获取数据库中记录数
        /// </summary>
        /// <param name="dbcode"></param>
        /// <returns></returns>
        public int GetDBFieldCount(string dbcode)
        {
            int count = 0;
    
            SYS_CMS_DATABASE result = null;
            string sql = @"select * from SYS_CMS_DATABASE where  DATABASECODE="+dbcode;
            IList<SYS_CMS_DATABASE> itemList = Context.ExecuteSql<SYS_CMS_DATABASE>(sql);
            if (itemList != null && itemList.Count > 0)
            {
               result = itemList[0];
               string sqlstr = "SELECT count(*) as sumfield FROM " + dbcode;
               if (result.SearchType == 0)
               {
                   sqlstr+="_METADATA";
               }
               DataTable dt = context.ExecuteSql(sqlstr);
               if (dt != null & dt.Rows.Count > 0)
               {
                   count = int.Parse(dt.Rows[0]["sumfield"].ToString());
               }
            }

         
            return count;
        }
    }
}
